package com.mask.example.controller;

import com.mask.token.annotation.RequireLogin;
import com.mask.token.common.Result;
import com.mask.token.model.MaskUserDetails;
import com.mask.token.util.MaskSecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户资料控制器示例
 * 展示如何使用MaskSecurityUtil工具类
 *
 * @author mask
 * @since 1.0.0
 */
@Slf4j
@RestController
@RequestMapping("/api/profile")
@RequireLogin
public class ProfileController {

    /**
     * 获取当前用户基本信息
     */
    @GetMapping("/info")
    public Result<Map<String, Object>> getCurrentUserInfo() {
        // 使用工具类获取用户信息
        Long userId = MaskSecurityUtil.getCurrentUserId();
        String username = MaskSecurityUtil.getCurrentUsername();
        String email = MaskSecurityUtil.getCurrentUserEmail();
        String phone = MaskSecurityUtil.getCurrentUserPhone();
        String nickname = MaskSecurityUtil.getCurrentUserNickname();
        String avatar = MaskSecurityUtil.getCurrentUserAvatar();
        String deviceId = MaskSecurityUtil.getCurrentUserDeviceId();
        String role = MaskSecurityUtil.getCurrentUserRole();
        java.time.Instant loginTime = MaskSecurityUtil.getCurrentUserLoginTime();
        java.time.Instant lastActiveTime = MaskSecurityUtil.getCurrentUserLastActiveTime();

        Map<String, Object> userInfo = new HashMap<>();
        userInfo.put("userId", userId);
        userInfo.put("username", username);
        userInfo.put("email", email);
        userInfo.put("phone", phone);
        userInfo.put("nickname", nickname);
        userInfo.put("avatar", avatar);
        userInfo.put("deviceId", deviceId);
        userInfo.put("role", role);
        userInfo.put("loginTime", loginTime);
        userInfo.put("lastActiveTime", lastActiveTime);

        return Result.success("获取用户信息成功", userInfo);
    }

    /**
     * 获取当前用户完整详情
     */
    @GetMapping("/details")
    public Result<MaskUserDetails> getCurrentUserDetails() {
        // 直接获取完整的用户详情对象
        MaskUserDetails userDetails = MaskSecurityUtil.getCurrentUser();
        
        if (userDetails == null) {
            return Result.fail(401, "用户未认证");
        }

        return Result.success("获取用户详情成功", userDetails);
    }

    /**
     * 获取当前用户角色和权限
     */
    @GetMapping("/authorities")
    public Result<Map<String, Object>> getCurrentUserAuthorities() {
        List<String> roles = MaskSecurityUtil.getCurrentUserRoles();
        List<String> permissions = MaskSecurityUtil.getCurrentUserPermissions();

        Map<String, Object> authorities = new HashMap<>();
        authorities.put("roles", roles);
        authorities.put("permissions", permissions);

        return Result.success("获取用户权限成功", authorities);
    }

    /**
     * 检查当前用户是否具有指定角色
     */
    @GetMapping("/check-role/{role}")
    public Result<Boolean> checkRole(@PathVariable String role) {
        boolean hasRole = MaskSecurityUtil.hasRole(role);
        return Result.success("角色检查完成", hasRole);
    }

    /**
     * 检查当前用户是否具有指定权限
     */
    @GetMapping("/check-permission/{permission}")
    public Result<Boolean> checkPermission(@PathVariable String permission) {
        boolean hasPermission = MaskSecurityUtil.hasPermission(permission);
        return Result.success("权限检查完成", hasPermission);
    }

    /**
     * 检查当前用户是否具有任意一个指定角色
     */
    @PostMapping("/check-any-role")
    public Result<Boolean> checkAnyRole(@RequestBody List<String> roles) {
        boolean hasAnyRole = MaskSecurityUtil.hasAnyRole(roles.toArray(new String[0]));
        return Result.success("角色检查完成", hasAnyRole);
    }

    /**
     * 检查当前用户是否具有任意一个指定权限
     */
    @PostMapping("/check-any-permission")
    public Result<Boolean> checkAnyPermission(@RequestBody List<String> permissions) {
        boolean hasAnyPermission = MaskSecurityUtil.hasAnyPermission(permissions.toArray(new String[0]));
        return Result.success("权限检查完成", hasAnyPermission);
    }

    /**
     * 获取当前用户扩展属性
     */
    @GetMapping("/attribute/{key}")
    public Result<Object> getUserAttribute(@PathVariable String key) {
        Object value = MaskSecurityUtil.getCurrentUserAttribute(key);
        return Result.success("获取用户属性成功", value);
    }

    /**
     * 更新当前用户最后活跃时间
     */
    @PostMapping("/update-last-active")
    public Result<Void> updateLastActiveTime() {
        MaskSecurityUtil.updateCurrentUserLastActiveTime();
        return Result.success("更新最后活跃时间成功", null);
    }

    /**
     * 获取当前用户认证状态
     */
    @GetMapping("/auth-status")
    public Result<Map<String, Object>> getAuthStatus() {
        boolean isAuthenticated = MaskSecurityUtil.isAuthenticated();
        Long userId = MaskSecurityUtil.getCurrentUserId();
        String username = MaskSecurityUtil.getCurrentUsername();

        Map<String, Object> status = new HashMap<>();
        status.put("isAuthenticated", isAuthenticated);
        status.put("userId", userId);
        status.put("username", username);

        return Result.success("获取认证状态成功", status);
    }
}
